﻿using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.PLCController;

namespace Test
{
    public class Siemens1200PLC:IPLC
    {
        private SiemensS7Net SiemensS7Net;
        public PLCBaseInfo PLCBaseInfo { get; set; }
        public PLCConnectionInfo PLCConnectionInfo { get; set; }
        public bool isConnectSuccess { get; set ; }

        public Siemens1200PLC(PLCBaseInfo  pLCBaseInfo, PLCConnectionInfo pLCConnectionInfo)
        {
            PLCBaseInfo = pLCBaseInfo;
            PLCConnectionInfo = pLCConnectionInfo;
            switch (PLCBaseInfo.ControllertType)
            {
                case PLCType.Siemens_1500:
                    SiemensS7Net = new SiemensS7Net(SiemensPLCS.S1500);
                    break;
                case PLCType.Siemens_1200:
                    SiemensS7Net = new SiemensS7Net(SiemensPLCS.S1500);
                    break;
                case PLCType.Siemens_200Smart:
                    SiemensS7Net = new SiemensS7Net(SiemensPLCS.S200Smart);
                    break;
            }
        }

        public  async void Connect()
        {
            SiemensS7Net.IpAddress=PLCConnectionInfo.ControllerIP;
           var res= await SiemensS7Net.ConnectServerAsync();
           isConnectSuccess= res.IsSuccess;
        }

        public void DisConnect()
        {
            SiemensS7Net.ConnectClose();
        }
        
        public void AnalyseDataSheet()
        {
          var res=  PLCConnectionInfo.ContinuousData;
            foreach (var item in res) 
            {
                if(item.Key==0)
                {

                }
                else
                {
                    var name = item.Select(x => x.Address).FirstOrDefault();
                    var count = (ushort)item.Count();
                    ReadList(name, count);
                }

            }
        }


        public void Reader()
        {
           var res= SiemensS7Net.ReadInt16("MW100",2).Content;
           
        }

        public void Writer()
        {
            throw new NotImplementedException();
        }

        public void ReadList(string data,ushort num)
        {
            var res = SiemensS7Net.ReadInt16(data, num).Content;
        }



        public void ReadList()
        {
            throw new NotImplementedException();
        }

        public void WriteList()
        {
            throw new NotImplementedException();
        }


    }
}
